<template>
  <div class="home">
    <a-form
      :form="form"
      :label-col="{ span: 5 }"
      :wrapper-col="{ span: 12 }"
      @submit="handleSubmit"
    >
      <a-form-item label="城市" name="city">
        <a-cascader
          v-decorator="['city', { rules: [{ required: true, message: '城市必选' }] }]"
          :options="CascaderDataList"
          :fieldNames="{ label: 'areaName', value: 'id', children: 'children' }"
        />
      </a-form-item>
      <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
        <a-button type="primary" html-type="submit"> 提交 </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
// 级联选择演示demo

// 模拟数据源 只需把这个数据换成后台的就行
const CascaderDataList = [
  {
    areaName: "河南省",
    areaType: "province",
    id: "41010000001",
    children: [
      {
        areaName: "郑州市",
        areaType: "city",
        id: "41010000000",
        children: [],
      },
    ],
  },
];

export default {
  name: "Cascader",
  data() {
    return {
      CascaderDataList: [], // 级联选择数据
      form: this.$form.createForm(this, { name: "demoForm" }), // 初始化form表单
    };
  },
  mounted() {
    // mounted生命周期请求接口赋值
    this.query();
  },
  methods: {
    query() {
      //  请求接口 拿到级联选择数据源
      this.CascaderDataList = CascaderDataList;
    },
    handleSubmit(e) {
      // 表单提交函数
      console.log(e);
      e.preventDefault();
      this.form.validateFields((err, values) => {
        // 如果有err 则表单校验rule 不通过
        if (!err) {
          console.log("Received values of form: ", values);
        }
        // values 是表单的值 组合一下传给后台提交接口
        console.log('表单数据---', values);
      });
    },
  },
};
</script>
